<!DOCTYPE html>
<html>
<head>
	<title>Memlib</title>
	<script src="../../script/jq.js"></script>
	<script src="../../script/login_check.js"></script>
	<script src="../../script/scale-suit.js"></script>
	<script>
		var added = false;
		var nowWordlistName;
		$(function() {
			var show = 0;
			$("*").click(function () {
				setTimeout(function () {
					switch (show) {
						case 1:
							$("#wordlist-choose").css("display", "none");
							show = 0;
							break;
					}
				}, 20);
			});
			$(".public-wordlist-card").click(function () {
				if (show == 1) {
					$("#wordlist-choose").css("display", "none");
					show = 0;
				} else {
					nowWordlistName = $(this).children().eq(0).html();
					console.log(nowWordlistName);
					setTimeout(function () {
						$("#wordlist-choose").css("display", "block");
						show = 1;
					}, 40);
				}
			});
			var MaxCount = 0;
			$("#auto-add-count-yes").click(function (){
				if (added)
					return;
				let count = Number($(this).prev().val());
				if (isNaN(count) || count < 1) {
					$(this).prev().val("");
					$(this).prev().attr("placeholder","请输入一个正整数");
					return;
				}
				if (count > 50) {
					$(this).prev().val("");
					$(this).prev().attr("placeholder","太多辣!一次不超50个");
					return;
				}
				MaxCount = count;
				$(this).prev().prev().html("正在下载单词表，可能会有点慢哦");
				let p = $(this).prev().prev();
				$.ajaxSetup({async:false});
				$.post("ChoosePublicList.php",{
					list_name:nowWordlistName,
				},function(data){
					console.log(data);
					let split = data.indexOf("\n");
					let public_cache = data.substring(0,split);
					let public_cache_map = Object.create(null);
					data = data.substring(split+1);

					let addedCount = 0;
					let newlist = Object.create(null);
					let wl = JSON.parse(data);
					let keys = Object.keys(wl);
					let values = Object.values(wl);
					let cache = JSON.parse(public_cache);
					let caches = Object.values(cache);
					for (let i = 0; i<keys.length; i++) {
						if (addedCount < MaxCount) {
							let repeat = false;
							for (let j = 0; j < caches.length; j++) {
								if (keys[i] == caches[j])
									repeat = true;
							}
							if (!repeat) {
								newlist[values[i][0]] = values[i][1];
								public_cache_map[values[i][0]] = keys[i];
								//console.log(keys[i],values[i][0],values[i][1]);
								//console.log(newlist);
								caches.push(keys[i]);
								addedCount++
							}
						}
					}
					//console.log(newlist);
					let data1 = JSON.stringify(newlist);
					console.log(data1);
					let err = "";
					$.post("CreatePrivateList.php",{
						data:data1,
						public_cache_map:JSON.stringify(public_cache_map),
					},function(data2){
						if(data2 != "0"){
							err += data2+"(0)";
						}
						$.post("ChoosePublicList.php",{
							cache_word_ids:JSON.stringify(caches),
						},function(data3) {
							if(data3 != "0"){
								err += data2+"(1)";
							}
						});
					});

					if(err == "") {
						p.html("添加成功辣，2秒后这个小窗会关闭");
						added = true;
						p.next().val("");
						setTimeout(function () {
							$("#auto-add-count").css("display", "none");
							p.html("输入一个数字");
						}, 2000);
					} else {
						p.html(err);
					}

				});
				$.ajaxSetup({async:true});
				//$("#auto-add-count").css("display","none");
			});
			$("#auto-add-count-no").click(function (){
				$("#auto-add-count").css("display","none");
			});
			$("#wordlist-choose-auto-add").click(function (){
				$("#auto-add-count").css("display","block");
			});
		});
	</script>
	<link rel="stylesheet" type="text/css" href="../../style/style.css">
	<link rel="stylesheet" type="text/css" href="../../style/animation.css">
	<style>
		.public-wordlist-card{
			display: inline-block;
			height: 100px;
			background-color: #99c0ff;
			border-radius: 25px;
			font-size: 40px;
			margin-bottom: 10px;
		}
		.public-wordlist-name{
			display: block;
		}
		.public-wordlist-subname{
			margin-left: 400px;
			font-size: 28px;
		}
		#wordlist-choose, #auto-add-count{
			display: none;
			width: 450px;
			background-color: #ccccff;
			border-radius: 20px;
			border: 1px solid #abc;
			box-shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
			position: fixed;
			top: 23%;
			left: 0;
			right: 0;
			margin: 0 auto;
		}
		#wordlist-choose .button{
			border-radius: 20px;
			width: 450px;
		}
		.line{
			display: inline-block;
			width: 430px!important;
			height: 0;
			border: 2px solid rgba(0, 0, 0, 0.3);
		}
		#auto-add-count{
			padding: 20px 0;
		}
		#auto-add-count p{
			font-size: 30px;
		}
		#auto-add-count div{
			display: inline-block;
			height: 80px;
			width: 400px;
			line-height: 80px;
			font-size: 35px;
			margin-top: 10px;
			background-color: #5ffaff;
			border-radius: 30px;
		}
		#auto-add-count-input{
			height: 80px;
			width: 400px;
			margin-top: 30px;
			border-radius: 30px;
			text-align: center;
			font-size: 40px;
		}
	</style>
	<meta charset="utf-8">
<body>
	<div id="main">
		<div id="notice">请选择一个单词表</div>
		<div id="public-wordlist-container">
			<div class="public-wordlist-card cardbase hover-shadow">
				<p class="public-wordlist-name">四级考试大纲词表（2016版）</p>
				<p class="public-wordlist-subname">共6127词</p>
			</div>
			<div class="public-wordlist-card cardbase hover-shadow">
				<p class="public-wordlist-name">2019四级真题核心词汇</p>
				<p class="public-wordlist-subname">共1605词</p>
			</div>
			<div class="public-wordlist-card cardbase hover-shadow">
				<p class="public-wordlist-name">扇贝循环单词书·四级核心词汇</p>
				<p class="public-wordlist-subname">共1038词</p>
			</div>
			<div class="public-wordlist-card cardbase hover-shadow">
				<p class="public-wordlist-name">六级考试大纲词表（2016版）</p>
				<p class="public-wordlist-subname">共1715词</p>
			</div>
			<div class="public-wordlist-card cardbase hover-shadow">
				<p class="public-wordlist-name">2019六级真题核心词汇</p>
				<p class="public-wordlist-subname">共1596词</p>
			</div>
			<div class="public-wordlist-card cardbase hover-shadow">
				<p class="public-wordlist-name">扇贝循环单词书·六级（正序）</p>
				<p class="public-wordlist-subname">共1613词</p>
			</div>
			<div class="public-wordlist-card cardbase hover-shadow">
				<p class="public-wordlist-name">2018考研英语词汇大纲</p>
				<p class="public-wordlist-subname">共5519词</p>
			</div>
			<div class="public-wordlist-card cardbase hover-shadow">
				<p class="public-wordlist-name">2019考研真题核心词汇</p>
				<p class="public-wordlist-subname">共2549词</p>
			</div>
		</div>
		<div id="wordlist-choose">
			<div id="wordlist-choose-auto-add" class="button">自动添加</div>
			<div class="line"></div>
			<div id="wordlist-choose-user-add" class="button">手动添加</div>
		</div>
		<div id="auto-add-count">
			<p>要添加多少个词？ <br />建议一次20个左右，复习后再来</p>
			<input id="auto-add-count-input" type="text" name="add-count" placeholder="输入一个数字" required>
			<div id="auto-add-count-yes" class="hover-shadow">确定</div>
			<div id="auto-add-count-no" class="hover-shadow">取消</div>
		</div>
	</div>
</body>
</html>
